//#include <math.h>
//#include "types.h"

#if ENABLE_3DLUT
#include "global.h"
#include "drv3DLUT_C.h"


#define  ENABLE_3DLUT_DEBUG  0
#if (ENABLE_3DLUT_DEBUG&&ENABLE_DEBUG)
#define LUT_printData(str, value)   printData(str, value)
#define LUT_printMsg(str)               printMsg(str)
#else
#define LUT_printData(str, value)
#define LUT_printMsg(str)
#endif



#if ENABLE_3DLUT


BYTE code t3DHWGain[]=
{
    e3dLutDelta4X,
};

BYTE code sramIndexTable[2][9][9] = {
//odd layer(1, 3, 5, 7, 9)
{
	{1,2,1,2,1,2,1,2,1},
	{3,4,3,4,3,4,3,4,3},
	{1,2,1,2,1,2,1,2,1},
	{3,4,3,4,3,4,3,4,3},
	{1,2,1,2,1,2,1,2,1},
	{3,4,3,4,3,4,3,4,3},
	{1,2,1,2,1,2,1,2,1},
	{3,4,3,4,3,4,3,4,3},
	{1,2,1,2,1,2,1,2,1}
},
//even layer(2, 4, 6, 8)
{
	{5,6,5,6,5,6,5,6,5},
	{7,8,7,8,7,8,7,8,7},
	{5,6,5,6,5,6,5,6,5},
	{7,8,7,8,7,8,7,8,7},
	{5,6,5,6,5,6,5,6,5},
	{7,8,7,8,7,8,7,8,7},
	{5,6,5,6,5,6,5,6,5},
	{7,8,7,8,7,8,7,8,7},
	{5,6,5,6,5,6,5,6,5}
}
};

BYTE code sramAddrTable[9][9][9] = {
{//layer 1: B = 0x00
	{  0,  0,  1,  1,  2,  2,  3,  3, 80},
	{  0,  0,  1,  1,  2,  2,  3,  3, 80},
	{  4,  4,  5,  5,  6,  6,  7,  7, 81},
	{  4,  4,  5,  5,  6,  6,  7,  7, 81},
	{  8,  8,  9,  9, 10, 10, 11, 11, 82},
	{  8,  8,  9,  9, 10, 10, 11, 11, 82},
	{ 12, 12, 13, 13, 14, 14, 15, 15, 83},
	{ 12, 12, 13, 13, 14, 14, 15, 15, 83},
	{100, 80,101, 81,102, 82,103, 83,120}
},
{//layer 2: B = 0x20
	{  0,  0,  1,  1,  2,  2,  3,  3, 64},
	{  0,  0,  1,  1,  2,  2,  3,  3, 64},
	{  4,  4,  5,  5,  6,  6,  7,  7, 65},
	{  4,  4,  5,  5,  6,  6,  7,  7, 65},
	{  8,  8,  9,  9, 10, 10, 11, 11, 66},
	{  8,  8,  9,  9, 10, 10, 11, 11, 66},
	{ 12, 12, 13, 13, 14, 14, 15, 15, 67},
	{ 12, 12, 13, 13, 14, 14, 15, 15, 67},
	{ 80, 64, 81, 65, 82, 66, 83, 67, 96}
},
{//layer 3: B = 0x40
	{ 16, 16, 17, 17, 18, 18, 19, 19, 84},
	{ 16, 16, 17, 17, 18, 18, 19, 19, 84},
	{ 20, 20, 21, 21, 22, 22, 23, 23, 85},
	{ 20, 20, 21, 21, 22, 22, 23, 23, 85},
	{ 24, 24, 25, 25, 26, 26, 27, 27, 86},
	{ 24, 24, 25, 25, 26, 26, 27, 27, 86},
	{ 28, 28, 29, 29, 30, 30, 31, 31, 87},
	{ 28, 28, 29, 29, 30, 30, 31, 31, 87},
	{104, 84,105, 85,106, 86,107, 87,121}
},
{//layer 4: B = 0x60
	{ 16, 16, 17, 17, 18, 18, 19, 19, 68},
	{ 16, 16, 17, 17, 18, 18, 19, 19, 68},
	{ 20, 20, 21, 21, 22, 22, 23, 23, 69},
	{ 20, 20, 21, 21, 22, 22, 23, 23, 69},
	{ 24, 24, 25, 25, 26, 26, 27, 27, 70},
	{ 24, 24, 25, 25, 26, 26, 27, 27, 70},
	{ 28, 28, 29, 29, 30, 30, 31, 31, 71},
	{ 28, 28, 29, 29, 30, 30, 31, 31, 71},
	{ 84, 68, 85, 69, 86, 70, 87, 71, 97}
},
	{//layer 5: B = 0x80
	{ 32, 32, 33, 33, 34, 34, 35, 35, 88},
	{ 32, 32, 33, 33, 34, 34, 35, 35, 88},
	{ 36, 36, 37, 37, 38, 38, 39, 39, 89},
	{ 36, 36, 37, 37, 38, 38, 39, 39, 89},
	{ 40, 40, 41, 41, 42, 42, 43, 43, 90},
	{ 40, 40, 41, 41, 42, 42, 43, 43, 90},
	{ 44, 44, 45, 45, 46, 46, 47, 47, 91},
	{ 44, 44, 45, 45, 46, 46, 47, 47, 91},
	{108, 88,109, 89,110, 90,111, 91,122}
},
{//layer 6: B = 0xA0
	{ 32, 32, 33, 33, 34, 34, 35, 35, 72},
	{ 32, 32, 33, 33, 34, 34, 35, 35, 72},
	{ 36, 36, 37, 37, 38, 38, 39, 39, 73},
	{ 36, 36, 37, 37, 38, 38, 39, 39, 73},
	{ 40, 40, 41, 41, 42, 42, 43, 43, 74},
	{ 40, 40, 41, 41, 42, 42, 43, 43, 74},
	{ 44, 44, 45, 45, 46, 46, 47, 47, 75},
	{ 44, 44, 45, 45, 46, 46, 47, 47, 75},
	{ 88, 72, 89, 73, 90, 74, 91, 75, 98}
},
	{//layer 7: B = 0xC0
	{ 48, 48, 49, 49, 50, 50, 51, 51, 92},
	{ 48, 48, 49, 49, 50, 50, 51, 51, 92},
	{ 52, 52, 53, 53, 54, 54, 55, 55, 93},
	{ 52, 52, 53, 53, 54, 54, 55, 55, 93},
	{ 56, 56, 57, 57, 58, 58, 59, 59, 94},
	{ 56, 56, 57, 57, 58, 58, 59, 59, 94},
	{ 60, 60, 61, 61, 62, 62, 63, 63, 95},
	{ 60, 60, 61, 61, 62, 62, 63, 63, 95},
	{112, 92,113, 93,114, 94,115, 95,123}
},
{//layer 8: B = 0xE0
	{ 48, 48, 49, 49, 50, 50, 51, 51, 76},
	{ 48, 48, 49, 49, 50, 50, 51, 51, 76},
	{ 52, 52, 53, 53, 54, 54, 55, 55, 77},
	{ 52, 52, 53, 53, 54, 54, 55, 55, 77},
	{ 56, 56, 57, 57, 58, 58, 59, 59, 78},
	{ 56, 56, 57, 57, 58, 58, 59, 59, 78},
	{ 60, 60, 61, 61, 62, 62, 63, 63, 79},
	{ 60, 60, 61, 61, 62, 62, 63, 63, 79},
	{ 92, 76, 93, 77, 94, 78, 95, 79, 99}
},
	{//layer 9: B = 0x100
	{ 64, 64, 65, 65, 66, 66, 67, 67, 96},
	{ 64, 64, 65, 65, 66, 66, 67, 67, 96},
	{ 68, 68, 69, 69, 70, 70, 71, 71, 97},
	{ 68, 68, 69, 69, 70, 70, 71, 71, 97},
	{ 72, 72, 73, 73, 74, 74, 75, 75, 98},
	{ 72, 72, 73, 73, 74, 74, 75, 75, 98},
	{ 76, 76, 77, 77, 78, 78, 79, 79, 99},
	{ 76, 76, 77, 77, 78, 78, 79, 79, 99},
	{116, 96,117, 97,118, 98,119, 99,124},
},
};

BYTE code t3DSRAMTblSize[8] =
{
    125, 100, 100,  80, 100,  80,  80,  64,
};

BYTE code t3DSRAMMTbl[3][729] =
{
    {
        0x00, 0x04, 0x24, 0x10, 0x00, 0x8E, 0x0C, 0x12, 0x00, 0xC0, 0x96, 0x08, 0x02, 0xC0, 0xFF, 0x94,
        0x00, 0x08, 0x16, 0x10, 0x00, 0x90, 0x02, 0x12, 0x00, 0xC0, 0x96, 0x06, 0x0C, 0xC0, 0xF6, 0x98,
        0x00, 0x02, 0x1A, 0x10, 0x00, 0x90, 0x02, 0x12, 0x0C, 0xC0, 0x9A, 0x00, 0x00, 0xC0, 0xB8, 0x9A,
        0x00, 0x86, 0x14, 0x0E, 0x00, 0x92, 0x82, 0x10, 0x00, 0xC0, 0x9A, 0x84, 0x00, 0xC0, 0xBE, 0xA0,
        0x00, 0x8A, 0x0E, 0x0E, 0x00, 0x94, 0x86, 0x10, 0x00, 0xC0, 0x9C, 0x86, 0x00, 0xC0, 0xC4, 0xA0,
        0xAF, 0xAD, 0xAD, 0xB1, 0xAF, 0xAD, 0xAD, 0xAF, 0xAF, 0xAD, 0xAD, 0xAF, 0xB1, 0xAF, 0xAD, 0xAD,
        0xB1, 0xAF, 0xAD, 0xAF, 0x00, 0xC0, 0xFF, 0xE4, 0x0A, 0xC0, 0xFF, 0xF2, 0x02, 0xC0, 0xD6, 0xE4,
        0x02, 0xC0, 0xCC, 0xBE, 0x00, 0xC0, 0xF6, 0xC0, 0xB1, 0xB1, 0xB1, 0xB1, 0xAF, 0x86, 0x0E, 0x2E,
        0x90, 0xA0, 0x00, 0x1A, 0x8E, 0xA0, 0xC0, 0x88, 0x8E, 0x9E, 0xE0, 0xB6, 0x92, 0x9E, 0x10, 0x22,
        0x90, 0x94, 0x88, 0x0C, 0x8E, 0x9E, 0xC2, 0x8A, 0x8E, 0xA0, 0xE0, 0xC0, 0x90, 0xA0, 0x0C, 0x26,
        0x90, 0xA0, 0x88, 0x0A, 0x8E, 0xA0, 0xA4, 0x90, 0x8E, 0xA0, 0xE0, 0xC0, 0x90, 0xA0, 0x08, 0x22,
        0x92, 0xA0, 0x88, 0x08, 0x90, 0xA0, 0xA8, 0x90, 0x8E, 0x90, 0xD4, 0xB0, 0x94, 0xA0, 0x04, 0x1C,
        0x92, 0xA0, 0x8C, 0x04, 0x90, 0xA0, 0xA8, 0x92, 0x8E, 0xA0, 0xE0, 0xB0, 0x92, 0xA0, 0xE0, 0xFF,
        0xA8, 0xA0, 0xE0, 0xFF, 0xB4, 0xA0, 0xE0, 0xD6, 0xC0, 0xA0, 0xE0, 0xC2, 0xC2, 0xA0, 0xE0, 0xD6,
        0xAA, 0x00, 0x82, 0x14, 0x10, 0x02, 0xC0, 0x84, 0x12, 0x02, 0xC0, 0xB2, 0x88, 0x00, 0xC0, 0xFF,
        0xB2, 0x00, 0x86, 0x0A, 0x10, 0x00, 0xA4, 0x88, 0x10, 0x10, 0xC0, 0xCA, 0x8A, 0x06, 0xC0, 0xFF,
        0xB6, 0x00, 0x8E, 0x08, 0x10, 0x00, 0x9E, 0x8C, 0x0A, 0x10, 0xB4, 0xAA, 0x8C, 0x00, 0xC0, 0xCA,
        0xCA, 0x00, 0x94, 0x06, 0x0E, 0x00, 0xC0, 0x8E, 0x08, 0x10, 0xC0, 0xB0, 0x92, 0x00, 0xC0, 0xBC,
        0xB0, 0x00, 0x98, 0x02, 0x0E, 0x00, 0xC0, 0x90, 0x06, 0x00, 0xC0, 0xAA, 0x92, 0x00, 0xC0, 0xEA,
        0xB8, 0xAF, 0xAD, 0xAF, 0xB1, 0xAF, 0xAD, 0xAF, 0xB1, 0xAF, 0xAD, 0xAF, 0xB1, 0xB1, 0xAD, 0xAD,
        0xAF, 0xB1, 0xAD, 0xAF, 0xB1, 0xA0, 0x08, 0x20, 0x8E, 0xA0, 0x92, 0x08, 0x8E, 0xA0, 0xE0, 0x96,
        0x8E, 0xA0, 0xE0, 0xEE, 0x94, 0xA0, 0x02, 0x16, 0x90, 0xA0, 0x94, 0x04, 0x8E, 0x96, 0xD6, 0x98,
        0x90, 0xA0, 0xE0, 0xF6, 0x96, 0x9E, 0x82, 0x16, 0x90, 0xA0, 0x94, 0x82, 0x8E, 0x9E, 0xAE, 0x9A,
        0x8E, 0xA0, 0xD0, 0xCC, 0x98, 0xA0, 0x86, 0x12, 0x92, 0xA0, 0x96, 0x84, 0x8E, 0xA0, 0xC8, 0x9C,
        0x8E, 0xA0, 0xE0, 0xB4, 0x9E, 0xA0, 0x8A, 0x0E, 0x92, 0xA0, 0x98, 0x88, 0x8E, 0xA0, 0xE0, 0x9C,
        0x90, 0xA0, 0xE0, 0xD2, 0x9C, 0x00, 0x06, 0x14, 0x10, 0x0C, 0x92, 0x08, 0x12, 0x06, 0xBE, 0x96,
        0x06, 0x00, 0xC0, 0xFF, 0x98, 0x02, 0x06, 0x1A, 0x10, 0x12, 0x90, 0x00, 0x12, 0x00, 0xC0, 0x98,
        0x04, 0x14, 0xC0, 0xCE, 0x9A, 0x00, 0x84, 0x16, 0x0E, 0x00, 0x90, 0x00, 0x12, 0x00, 0xB2, 0x98,
        0x00, 0x0C, 0xB2, 0xB2, 0x9E, 0x00, 0x88, 0x12, 0x0E, 0x00, 0x92, 0x82, 0x10, 0x00, 0xC0, 0x9A,
        0x84, 0x10, 0xC0, 0xD0, 0xA2, 0xAD, 0xAD, 0xAD, 0xAF, 0xAF, 0xAD, 0xAD, 0xB1, 0xB1, 0xAF, 0xAD,
        0xAF, 0xB1, 0xAF, 0xAD, 0xAF, 0x00, 0xC0, 0xFF, 0xE8, 0x04, 0xBC, 0xFF, 0xF2, 0x00, 0xC0, 0xDC,
        0xD0, 0x00, 0xC0, 0xCC, 0xB4, 0xB1, 0xB1, 0xB1, 0xAF, 0x84, 0x0C, 0x24, 0x8E, 0xA0, 0x82, 0x14,
        0x8E, 0xA0, 0xD8, 0x8A, 0x8E, 0xA0, 0xE0, 0xB8, 0x90, 0x9E, 0x10, 0x26, 0x90, 0x8C, 0x88, 0x0A,
        0x8E, 0xA0, 0xA6, 0x8C, 0x8E, 0xA0, 0xE0, 0xCE, 0x90, 0xA0, 0x0A, 0x24, 0x90, 0x88, 0x8A, 0x0A,
        0x8E, 0x8A, 0xAA, 0x90, 0x8E, 0xA0, 0xBA, 0xAC, 0x90, 0xA0, 0x06, 0x1E, 0x92, 0xA0, 0x8A, 0x06,
        0x90, 0xA0, 0xA4, 0x92, 0x8E, 0xA0, 0xE0, 0xB6, 0x94, 0xA0, 0xE0, 0xFF, 0xB2, 0x94, 0xD0, 0xF2,
        0xB6, 0x9E, 0xE0, 0xCA, 0xCC, 0xA0, 0xE0, 0xBE, 0xAC, 0x00, 0x86, 0x0C, 0x12, 0x00, 0xBE, 0x86,
        0x12, 0x00, 0xC0, 0xC2, 0x8A, 0x00, 0xC0, 0xFF, 0xB6, 0x00, 0x8A, 0x0A, 0x10, 0x14, 0xA6, 0x8C,
        0x0C, 0x00, 0xC0, 0xC2, 0x8C, 0x02, 0xB6, 0xDE, 0xC0, 0x00, 0x92, 0x06, 0x10, 0x00, 0xA2, 0x8E,
        0x0A, 0x0E, 0xC0, 0xA6, 0x92, 0x00, 0xC0, 0xCC, 0xC0, 0x00, 0x98, 0x04, 0x0E, 0x00, 0xC0, 0x90,
        0x08, 0x04, 0xB6, 0xAE, 0x90, 0x00, 0xC0, 0xDA, 0xAE, 0xAD, 0xAD, 0xAD, 0xAF, 0xAF, 0xAD, 0xAD,
        0xAF, 0xB1, 0xAD, 0xAD, 0xB1, 0xB1, 0xAD, 0xAF, 0xAF, 0x94, 0x04, 0x1E, 0x8E, 0xA0, 0x94, 0x08,
        0x8E, 0xA0, 0xE0, 0x96, 0x90, 0xA0, 0xE0, 0xF6, 0x96, 0x9E, 0x02, 0x16, 0x90, 0xA0, 0x92, 0x00,
        0x8E, 0x96, 0xBA, 0x98, 0x90, 0xA0, 0xE0, 0xE4, 0x98, 0x9E, 0x84, 0x14, 0x90, 0xA0, 0x96, 0x84,
        0x8E, 0x86, 0xBE, 0x9A, 0x90, 0xA0, 0xE0, 0xBC, 0x98, 0xA0, 0x88, 0x10, 0x92, 0xA0, 0x96, 0x86,
        0x8E, 0xA0, 0xC8, 0x9A, 0x90, 0xA0, 0xE0, 0xC0, 0x9C,
    },
    {
        0x00, 0x00, 0x00, 0x0E, 0x84, 0x8E, 0x96, 0xA2, 0x82, 0x04, 0x92, 0x9A, 0x02, 0x06, 0x0C, 0x92,
        0x00, 0x00, 0x0C, 0x06, 0x88, 0x92, 0x96, 0xA4, 0x8A, 0x8C, 0x94, 0x9C, 0x82, 0x86, 0x02, 0x94,
        0x00, 0x00, 0x00, 0x00, 0x92, 0x9C, 0xAE, 0xC0, 0x84, 0x8E, 0x98, 0x9E, 0x86, 0x8C, 0x8E, 0x96,
        0x00, 0x00, 0x00, 0x00, 0x98, 0xA4, 0xC0, 0xC0, 0x88, 0x94, 0x9A, 0xA0, 0x00, 0x86, 0x90, 0x9E,
        0x00, 0x00, 0x00, 0x00, 0x9C, 0xA8, 0xC0, 0xC0, 0x90, 0x98, 0x9C, 0xA2, 0x00, 0x88, 0x94, 0x9C,
        0x0E, 0xAA, 0xA0, 0x98, 0x04, 0xAE, 0xA2, 0x9C, 0x00, 0xC0, 0xA4, 0x9E, 0x00, 0xC0, 0xA8, 0xA2,
        0x00, 0xC0, 0xAA, 0xA4, 0xAD, 0xAD, 0xAD, 0xAD, 0xAF, 0xAD, 0xAD, 0xAD, 0xAF, 0xAF, 0xAD, 0xAD,
        0xAF, 0xAF, 0xAF, 0xAD, 0xAF, 0xAF, 0xAF, 0xAF, 0xAD, 0xAD, 0xAD, 0xAD, 0xB3, 0x00, 0x00, 0x00,
        0x0E, 0x82, 0x92, 0x9E, 0xA6, 0x82, 0x84, 0x96, 0x9E, 0x02, 0x0C, 0x86, 0x94, 0x00, 0x06, 0x0E,
        0x0A, 0x8E, 0x94, 0x9C, 0xAA, 0x8C, 0x88, 0x98, 0xA0, 0x82, 0x84, 0x86, 0x98, 0x00, 0x00, 0x00,
        0x00, 0x9A, 0x9E, 0xB2, 0xBA, 0x88, 0x96, 0x9C, 0xA2, 0x8A, 0x8C, 0x8A, 0x9A, 0x00, 0x00, 0x00,
        0x00, 0x9C, 0xC0, 0xC0, 0xC0, 0x8E, 0x98, 0x9E, 0xA4, 0x84, 0x8A, 0x98, 0xA2, 0x00, 0x00, 0x00,
        0x00, 0xA0, 0xC0, 0xC0, 0xC0, 0x94, 0x9A, 0xA0, 0xA6, 0x84, 0x8E, 0x9A, 0xA0, 0xAD, 0xAD, 0xAF,
        0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAF, 0xAF, 0xAD, 0xAD, 0xAF, 0xAF, 0xAF, 0xAD, 0xAF, 0xAF, 0xAF,
        0xB3, 0x8C, 0xA0, 0xA0, 0x90, 0x84, 0x02, 0x94, 0x9C, 0x82, 0x04, 0x8A, 0x96, 0x8E, 0x8E, 0x8E,
        0x8E, 0xA0, 0xA0, 0xA0, 0x92, 0x8A, 0x8E, 0x96, 0x9C, 0x88, 0x8A, 0x84, 0x98, 0x90, 0x8E, 0x8E,
        0x8E, 0xA0, 0xA0, 0xA0, 0xA0, 0x86, 0x94, 0x9C, 0xA0, 0x88, 0x90, 0x94, 0x9A, 0x90, 0x90, 0x8E,
        0x8E, 0xA0, 0xA0, 0xA0, 0xA0, 0x8E, 0x96, 0x9E, 0xA2, 0x82, 0x8A, 0x96, 0x9E, 0x90, 0x90, 0x92,
        0x96, 0xA0, 0xA0, 0xA0, 0xA0, 0x94, 0x98, 0xA0, 0xA6, 0x8A, 0x92, 0x9A, 0x9E, 0x90, 0x90, 0x90,
        0x96, 0x90, 0xA2, 0x9C, 0x94, 0x92, 0xA4, 0x9E, 0x96, 0xA0, 0xA6, 0xA0, 0x98, 0xA0, 0xAC, 0xA4,
        0x9C, 0xA0, 0xAE, 0xA4, 0xA2, 0x8E, 0xA0, 0x98, 0xA0, 0x82, 0x90, 0x98, 0xA0, 0x00, 0x0A, 0x92,
        0x9A, 0x8E, 0x8E, 0x8E, 0x92, 0xA0, 0xA0, 0xA0, 0x96, 0x8C, 0x92, 0x9A, 0xA0, 0x8A, 0x88, 0x94,
        0x9C, 0x8E, 0x8E, 0x8E, 0x94, 0xA0, 0xA0, 0xA0, 0xA0, 0x8C, 0x98, 0x9C, 0xA4, 0x8C, 0x92, 0x9A,
        0x9E, 0x90, 0x90, 0x8E, 0x96, 0xA0, 0xA0, 0xA0, 0xA0, 0x94, 0x9A, 0xA2, 0xA8, 0x86, 0x90, 0x9C,
        0xA0, 0x90, 0x90, 0x94, 0x9C, 0xA0, 0xA0, 0xA0, 0xA0, 0x98, 0x9C, 0xA2, 0xAC, 0x8E, 0x96, 0x9C,
        0xA2, 0x90, 0x90, 0x90, 0xA0, 0x00, 0x00, 0x06, 0x10, 0x8A, 0x8E, 0x98, 0xA2, 0x86, 0x86, 0x94,
        0x9A, 0x02, 0x00, 0x0A, 0x92, 0x00, 0x00, 0x00, 0x00, 0x8A, 0x94, 0x9A, 0xA8, 0x8A, 0x90, 0x98,
        0x9E, 0x86, 0x88, 0x86, 0x94, 0x00, 0x00, 0x00, 0x00, 0x96, 0xA4, 0xB8, 0xC0, 0x84, 0x8E, 0x9C,
        0xA0, 0x84, 0x8C, 0x92, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x9A, 0xA6, 0xC0, 0xC0, 0x8C, 0x96, 0x9C,
        0xA2, 0x02, 0x84, 0x8E, 0x9E, 0x00, 0xAA, 0xA2, 0x9A, 0x04, 0xBC, 0xA2, 0x9C, 0x00, 0xB6, 0xA6,
        0xA0, 0x00, 0xC0, 0xA8, 0xA2, 0xAD, 0xAD, 0xAD, 0xAD, 0xAF, 0xAF, 0xAD, 0xAD, 0xAF, 0xAF, 0xAD,
        0xAD, 0xAF, 0xAF, 0xAF, 0xAF, 0xAD, 0xAD, 0xAD, 0xB3, 0x00, 0x00, 0x0E, 0x00, 0x8A, 0x94, 0x9A,
        0xA6, 0x88, 0x00, 0x96, 0x9E, 0x00, 0x04, 0x88, 0x96, 0x00, 0x00, 0x00, 0x00, 0x92, 0x98, 0xA0,
        0xAC, 0x8E, 0x92, 0x98, 0xA0, 0x86, 0x88, 0x82, 0x98, 0x00, 0x00, 0x00, 0x00, 0x9A, 0xA6, 0xBC,
        0xC0, 0x8A, 0x96, 0x9E, 0xA2, 0x86, 0x90, 0x96, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x9E, 0xC0, 0xC0,
        0xC0, 0x92, 0x9A, 0x9E, 0xA6, 0x00, 0x8A, 0x96, 0xA2, 0xAD, 0xAD, 0xAF, 0xAD, 0xAF, 0xAF, 0xAD,
        0xAD, 0xAF, 0xAF, 0xAD, 0xAD, 0xAF, 0xAF, 0xAF, 0xB1, 0x90, 0x9A, 0xA0, 0xA0, 0x86, 0x86, 0x94,
        0x9E, 0x84, 0x86, 0x86, 0x96, 0x8E, 0x8E, 0x8E, 0x8E, 0xA0, 0xA0, 0xA0, 0xA0, 0x86, 0x90, 0x98,
        0x9C, 0x8A, 0x8E, 0x8A, 0x98, 0x90, 0x90, 0x8E, 0x8E, 0xA0, 0xA0, 0xA0, 0xA0, 0x8C, 0x96, 0x9C,
        0xA4, 0x84, 0x8A, 0x98, 0x9E, 0x90, 0x90, 0x8E, 0x8E, 0xA0, 0xA0, 0xA0, 0xA0, 0x92, 0x96, 0x9E,
        0xA6, 0x86, 0x90, 0x98, 0x9E, 0x90, 0x90, 0x90, 0x9A, 0xA0, 0xA2, 0x9E, 0x96, 0x98, 0xA4, 0xA0,
        0x98, 0xA0, 0xAA, 0xA2, 0x9A, 0xA0, 0xAE, 0xA4, 0xA2, 0x94, 0xA0, 0xA0, 0xA0, 0x88, 0x90, 0x9A,
        0xA0, 0x86, 0x02, 0x94, 0x9A, 0x8E, 0x8E, 0x8E, 0x92, 0xA0, 0xA0, 0xA0, 0x9C, 0x8C, 0x96, 0x9C,
        0xA2, 0x8E, 0x8E, 0x96, 0x9C, 0x90, 0x8E, 0x8E, 0x94, 0xA0, 0xA0, 0xA0, 0xA0, 0x90, 0x9A, 0xA0,
        0xA8, 0x88, 0x90, 0x9C, 0x9E, 0x90, 0x90, 0x90, 0x98, 0xA0, 0xA0, 0xA0, 0xA0, 0x96, 0x9C, 0xA2,
        0xAA, 0x8A, 0x94, 0x9C, 0xA0, 0x90, 0x90, 0x92, 0x9E,
    },
    {
        0x00, 0x00, 0x0C, 0x12, 0x00, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00, 0x12, 0x12, 0x0C, 0x02, 0x14,
        0x24, 0x16, 0x08, 0x02, 0x04, 0x8A, 0x90, 0x94, 0x0E, 0x98, 0xAC, 0xAC, 0x84, 0xAC, 0xAE, 0xAC,
        0x4E, 0x48, 0x38, 0x28, 0x52, 0x20, 0x14, 0x0E, 0x0E, 0x08, 0x02, 0x8C, 0x1A, 0x12, 0x94, 0xA6,
        0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x22, 0x1A, 0x1E, 0x18, 0x12, 0x12,
        0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
        0x12, 0x10, 0x0C, 0x18, 0x84, 0x9C, 0xB2, 0xAA, 0x1E, 0x04, 0x94, 0xAE, 0x2C, 0x26, 0x14, 0x84,
        0x93, 0x93, 0x93, 0x93, 0x00, 0x00, 0x00, 0x12, 0x98, 0xC0, 0xC0, 0xAC, 0x32, 0x00, 0xB2, 0xBC,
        0x18, 0x1C, 0x1A, 0x8C, 0x93, 0x93, 0x93, 0x93, 0x1C, 0xA4, 0xC2, 0xA0, 0x93, 0x00, 0x00, 0x12,
        0x12, 0x00, 0x00, 0x0C, 0x10, 0x02, 0x00, 0x00, 0x12, 0x12, 0x00, 0x12, 0x18, 0x1E, 0x0E, 0x04,
        0x82, 0x84, 0x8E, 0x92, 0x98, 0x88, 0xB4, 0xB0, 0xAC, 0xA0, 0xA6, 0xAE, 0xA8, 0x50, 0x40, 0x30,
        0x22, 0x40, 0x18, 0x10, 0x08, 0x0E, 0x08, 0x00, 0x90, 0x18, 0x84, 0xA6, 0xAA, 0x2C, 0x2C, 0x2C,
        0x2C, 0x2C, 0x2C, 0x2C, 0x2A, 0x2C, 0x2C, 0x1C, 0x16, 0x1A, 0x16, 0x16, 0x10, 0x93, 0x93, 0x93,
        0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x00, 0x00, 0x12,
        0x18, 0xC0, 0xC0, 0xC0, 0xAA, 0x1C, 0x9E, 0xA8, 0xCC, 0x1A, 0x1E, 0x08, 0xA2, 0x93, 0x93, 0x93,
        0x93, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x0E, 0x0C, 0x02, 0x0A, 0x12, 0x00, 0x00, 0x00,
        0x16, 0x22, 0x04, 0x84, 0x8A, 0x82, 0x96, 0xA2, 0xA2, 0x0C, 0xA6, 0xB6, 0xAC, 0x8E, 0xC0, 0xB2,
        0xAC, 0x46, 0x32, 0x24, 0x18, 0x32, 0x28, 0x0A, 0x00, 0x08, 0x02, 0x8C, 0x9A, 0x28, 0x10, 0xA0,
        0xCA, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x26, 0x22, 0x2C, 0x2C, 0x2A, 0x18, 0x12, 0x10, 0x0C,
        0x02, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
        0x93, 0x12, 0x0E, 0x14, 0x1A, 0x90, 0xA6, 0xAE, 0xA6, 0x12, 0x88, 0xA0, 0xBA, 0x2C, 0x1E, 0x0A,
        0x94, 0x93, 0x93, 0x93, 0x93, 0x00, 0x00, 0x12, 0x0A, 0x00, 0x00, 0x0C, 0x10, 0x02, 0x02, 0x02,
        0x12, 0x00, 0x00, 0x12, 0x1A, 0x0A, 0x82, 0x88, 0x8C, 0x8A, 0x98, 0xA0, 0xA6, 0x92, 0xA8, 0xC0,
        0xAA, 0xC0, 0xC0, 0xAE, 0xA8, 0x3A, 0x2A, 0x1E, 0x14, 0x30, 0x0E, 0x02, 0x84, 0x06, 0x86, 0x8C,
        0x9C, 0x1E, 0x90, 0xA4, 0xB4, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x24, 0x20, 0x2C, 0x2C, 0x1C,
        0x0C, 0x10, 0x10, 0x04, 0x02, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
        0x93, 0x93, 0x93, 0x93, 0x93, 0x0A, 0x84, 0x88, 0x8A, 0x92, 0xA0, 0xA0, 0x90, 0x9A, 0x9E, 0x9E,
        0x90, 0xA0, 0x90, 0xA0, 0x8C, 0x38, 0x2E, 0x20, 0x12, 0x2A, 0x08, 0x00, 0x86, 0x04, 0x88, 0x90,
        0x9E, 0x1A, 0x92, 0xA6, 0xB4, 0x4C, 0x4C, 0x46, 0x3C, 0x4C, 0x36, 0x2C, 0x22, 0x36, 0x32, 0x18,
        0x06, 0x0C, 0x0A, 0x82, 0x84, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
        0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x92, 0x90, 0x8E, 0x8C, 0x0A, 0x8C, 0xA4, 0xBA, 0x34, 0x1A, 0x00,
        0x9A, 0x0C, 0x0C, 0x0C, 0x06, 0xA0, 0xA0, 0xA0, 0x8E, 0x14, 0xAC, 0xB0, 0xCC, 0x2E, 0x24, 0x8E,
        0xA2, 0x0C, 0x0C, 0x08, 0x02, 0x84, 0xC4, 0xB2, 0x00, 0x04, 0x88, 0x88, 0x90, 0xA0, 0xA0, 0x96,
        0x90, 0x9E, 0x9E, 0xA0, 0x92, 0x98, 0xA0, 0x8E, 0x8A, 0x34, 0x26, 0x18, 0x0E, 0x24, 0x04, 0x84,
        0x8A, 0x00, 0x90, 0x9E, 0xA2, 0x0C, 0xA2, 0xCA, 0xBA, 0x4C, 0x4C, 0x40, 0x36, 0x42, 0x2E, 0x26,
        0x1E, 0x34, 0x2E, 0x12, 0x02, 0x0C, 0x06, 0x86, 0x98, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
        0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0xA0, 0xA0, 0x8C, 0x86, 0x8E, 0xBC, 0xB2,
        0xCA, 0x2C, 0x14, 0x9C, 0xCC, 0x0C, 0x0C, 0x08, 0x00, 0x86, 0x9A, 0x9C, 0x94, 0x92, 0xA0, 0xA0,
        0x8E, 0x98, 0x96, 0x9C, 0x8E, 0xA0, 0xA0, 0xA0, 0x8C, 0x3A, 0x1A, 0x0E, 0x06, 0x0A, 0x00, 0x88,
        0x92, 0x14, 0x86, 0xAA, 0xAA, 0x1A, 0xA0, 0xA8, 0xC8, 0x4C, 0x42, 0x38, 0x30, 0x4C, 0x3E, 0x1E,
        0x14, 0x18, 0x14, 0x10, 0x08, 0x1C, 0x1E, 0x00, 0xA4, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
        0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x94, 0x92, 0x90, 0x86, 0x82, 0x98, 0xB0,
        0xC2, 0x28, 0x0C, 0x8E, 0xA8, 0x0C, 0x0C, 0x0C, 0x08, 0x90, 0x9E, 0x96, 0x94, 0xA0, 0xA0, 0x96,
        0x94, 0xA0, 0xA0, 0x96, 0x8C, 0xA0, 0xA0, 0x8E, 0x86, 0x22, 0x12, 0x0A, 0x02, 0x06, 0x86, 0x8E,
        0x94, 0x10, 0x96, 0xA6, 0xAE, 0x82, 0xAE, 0xB8, 0xBE, 0x44, 0x3E, 0x34, 0x2C, 0x4C, 0x26, 0x1A,
        0x12, 0x14, 0x0E, 0x0A, 0x8A, 0x20, 0x1A, 0x90, 0xA2, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
        0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
    },
};

#define min(a, b) (((a) < (b)) ? (a) : (b))

void ms3DLutLoadTbl_x8Gain(void)
{

    WORD xdata idx;
    BYTE xdata u8NodeValue[3];    //rgb node value

    //ColorMode = 0;
    for (idx = 0; idx < 9*9*9; idx++)
    {
        u8NodeValue[0] = min( (idx % 9)*32, 255);
        u8NodeValue[1] = min( ((idx / 9) % 9) * 32, 255);
        u8NodeValue[2] = min((idx /(81)) *32, 255);

        //FindSramSettings(idx, &sramSel, &sramAddr);
        msWriteByte(SC9_81,  sramIndexTable[ (idx /(81)) % 2][(idx / 9) % 9][idx % 9] - 1);  //sram selection
        msWriteByte(SC9_80,  sramAddrTable[idx /(81)][(idx / 9) % 9][idx % 9]); //sram address
        msWrite2Byte(SC9_82,  (WORD)(t3DSRAMMTbl[0][idx]*2)); //R channel data
        msWrite2Byte(SC9_84,  (WORD)(t3DSRAMMTbl[1][idx]*2)); //G channel data
        msWrite2Byte(SC9_86,  (WORD)(t3DSRAMMTbl[2][idx]*2)); //B channel data

        //execute write command
        msWriteBit(SC9_81, 1, _BIT5);

    }


}

void ms3DLutLoadTbl(BYTE ColorMode)
{
    BYTE xdata sramSel;
    BYTE xdata sramAddr;
    WORD xdata TblBaseAddr;

    ColorMode = 0;

    if (t3DHWGain[ColorMode] == e3dLutDelta8X)
    {
        ms3DLutLoadTbl_x8Gain();
        return;
    }

    TblBaseAddr = 0;
    for(sramSel=0; sramSel<8; sramSel++)
    {
        msWriteByte(SC9_81,  sramSel);  //sram selection

        for(sramAddr=0; sramAddr<t3DSRAMTblSize[sramSel]; sramAddr++)
        {

            msWriteByte(SC9_80,  sramAddr); //sram address

            msWrite2Byte(SC9_82,  (WORD)(t3DSRAMMTbl[0][TblBaseAddr+sramAddr]*4)); //R channel data
            msWrite2Byte(SC9_84,  (WORD)(t3DSRAMMTbl[1][TblBaseAddr+sramAddr]*4)); //G channel data
            msWrite2Byte(SC9_86,  (WORD)(t3DSRAMMTbl[2][TblBaseAddr+sramAddr]*4)); //B channel data

    		//execute write command
            msWriteBit(SC9_81, 1, _BIT5);
        }
        TblBaseAddr += t3DSRAMTblSize[sramSel];
    }
}


#endif
#if ENABLE_3DLUT_Test

void drv3DLutWriteReadTest(void)
{
    BYTE xdata idxSram =0;
    BYTE xdata idxAddr = 0;

     for( idxSram = 0; idxSram < 8; idxSram ++ )
    {
         LUT_printData("@@@ Write idxSram=>%d",idxSram);
         msWriteByteMask(SC9_81, idxSram,BIT2|BIT1|BIT0);
         for( idxAddr = 0; idxAddr < 100; idxAddr++ )
        {
            LUT_printData("@@@ Write SRAM idxAddr=>%d",idxAddr);
            msWriteByte(SC9_80, idxAddr);
            msWriteBit(SC9_81, 1, _BIT5);  //Write
            msWrite2Byte(SC9_82, 0x3FF);
            msWrite2Byte(SC9_84, 0x00);
            msWrite2Byte(SC9_86, 0x177 );
            msWriteBit(SC9_81, 1, _BIT6);  //Read
            LUT_printData("### Read  R data=>%x",msRead2Byte(SC9_8A));
            LUT_printData("### Read  G data=>%x",msRead2Byte(SC9_8C));
            LUT_printData("### Read  B data=>%x",msRead2Byte(SC9_8E));
        }
    }
     msWriteBit(SC9_88, BIT0, BIT0);
}

#endif


void ms3DLutEnable(BYTE ucSrcIdx, BYTE enable)
{
    //rgb 3d lut sub window enable
    //rgb 3d lut main window enable
    BYTE xdata mask;
    if( ucSrcIdx== MAIN_WINDOW )
        mask = _BIT0;
    else
        mask = _BIT1;
    msWriteBit(SC9_88, enable, mask);
}

void ms3DLutSetHardwareGain(BYTE ColorMode)
{
    BYTE xdata RegValue;

    RegValue = msReadByte(SC9_88) & (~(_BIT2 | _BIT3));
    RegValue = RegValue | (t3DHWGain[ColorMode]<<2);

    msWriteByte(SC9_88, RegValue);
}

void drv3DLutInit()
{
    //execute all sram initial write as 0; hardware auto clear after operation ready
    msWriteBit(SC9_81, 1, _BIT4);
}

void ms3DLutGrayGuard(BYTE enable)
{
    BYTE xdata mask;
    mask = _BIT6;

    if (enable == TRUE)
    {
        msWrite2Byte(SC9_90, 0x3F00);
    }

    msWriteBit(SC9_88, enable, mask);
}


#if ENABLE_3DLUT_Test
void mStar_Setup3DLUT( void )
{
    //printData("mStar_Setup3DLUT");

    drv3DLutInit();
    ms3DLutLoadTbl(0);
    ms3DLutSetHardwareGain(0);
    ms3DLutEnable(0, 0);
}
#endif

#endif
